<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Account_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function insertData($data){
    	  $this->db->query("call app_entity_ins(?,?,?,?,?,?,?,?,?,?,@id,@err,@msg)",$data);
	      $que = $this->db->query("SELECT @id as id,@err as err,@msg as msg");
	      $id; $msg; $err;
	        foreach($que->result() as $keys){
	          $msg = $keys->msg;
	          $err = $keys->err;
	          $id = $keys->id;
	        }
	        $arr = array(
	            'msg'   =>$msg,
	            'err'   =>$err,
	            'id'	=>$id
	        );
	      echo json_encode($arr);
    }

    public function updateData($data){
    	  $this->db->query("call app_entity_upd(?,?,?,?,?,?,?,?,?,?,?,@err,@msg)",$data);
	      $que = $this->db->query("SELECT @id as id,@err as err,@msg as msg");
	      $id; $msg; $err;
	        foreach($que->result() as $keys){
	          $msg = $keys->msg;
	          $err = $keys->err;
	        }
	        $arr = array(
	            'msg'   =>$msg,
	            'err'   =>$err
	        );
	      echo json_encode($arr);
    }

    public function insertDataAddr($data){
    	  $this->db->query("call app_entity_address_ins(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,@id,@err,@msg)",$data);
	      $que = $this->db->query("SELECT @id as id,@err as err,@msg as msg");
	      $id; $msg; $err;
	        foreach($que->result() as $keys){
	          $msg = $keys->msg;
	          $err = $keys->err;
	          $id  = $keys->id;
	        }
	        $arr = array(
	            'msg'   =>$msg,
	            'err'   =>$err,
	            'id'	=>$id
	        );
	      echo json_encode($arr);
    }

    public function updateDataAddr($data){
    	  $this->db->query("call app_entity_address_upd(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,@err,@msg)",$data);
	      $que = $this->db->query("SELECT @err as err,@msg as msg");
	      $id; $msg; $err;
	        foreach($que->result() as $keys){
	          $msg = $keys->msg;
	          $err = $keys->err;
	        }
	        $arr = array(
	            'msg'   =>$msg,
	            'err'   =>$err
	        );
	      echo json_encode($arr);
    }

    public function insertDataAcc($data){
    	  $this->db->query("call app_account_ins(?,?,?,?,?,?,?,?,?,?,?,?,?,?,@id,@err,@msg)",$data);
	      $que = $this->db->query("SELECT @id as id,@err as err,@msg as msg");
	      $id; $msg; $err;
	        foreach($que->result() as $keys){
	          $msg = $keys->msg;
	          $err = $keys->err;
	          $id = $keys->id;
	        }
	        $arr = array(
	            'msg'   =>$msg,
	            'err'   =>$err,
	            'id'	=>$id
	        );
	      echo json_encode($arr);
    }

    public function updateDataAcc($data){
    	  $this->db->query("call app_account_upd(?,?,?,?,?,?,?,?,?,?,?,?,@err,@msg)",$data);
	      $que = $this->db->query("SELECT @err as err,@msg as msg");
	      $id; $msg; $err;
	        foreach($que->result() as $keys){
	          $msg = $keys->msg;
	          $err = $keys->err;
	        }
	        $arr = array(
	            'msg'   =>$msg,
	            'err'   =>$err
	        );
	      echo json_encode($arr);
    }

    public function insertDataDev($data){
    	  $this->db->query("call app_device_ins(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,@id,@err,@msg)",$data);
	      $que = $this->db->query("SELECT @id as id,@err as err,@msg as msg");
	      $id; $msg; $err;
	        foreach($que->result() as $keys){
	          $msg = $keys->msg;
	          $err = $keys->err;
	          $id = $keys->id;
	        }
	        $arr = array(
	            'msg'   =>$msg,
	            'err'   =>$err,
	            'id'	=>$id
	        );
	      echo json_encode($arr);
    }

    public function updateDataDev($data){
    	  $this->db->query("call app_device_upd(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,@err,@msg)",$data);
	      $que = $this->db->query("SELECT @id as id,@err as err,@msg as msg");
	      $id; $msg; $err;
	        foreach($que->result() as $keys){
	          $msg = $keys->msg;
	          $err = $keys->err;
	        }
	        $arr = array(
	            'msg'   =>$msg,
	            'err'   =>$err
	        );
	      echo json_encode($arr);
    }

    public function getDevice($id){

    	$this->db->where('account_id',$id);
    	$query = $this->db->get('app_device_v');
    	return $query->result();

    }

    public function countries(){
    	$query = $this->db->get('app_countries');
    	return $query->result();
    }

    public function getDevice2($id){
    	$this->db->where('device_assignment_id',$id);
    	$query = $this->db->get('app_device_v');
    	return $query->result();
    }

    public function getEntities($id){
    	$this->db->where('entity_id',$id);
    	$query = $this->db->get('entity_address_v');
    	return $query->result();
    }

     public function getAddr($id,$acode){
    	$this->db->where('entity_id',$id);
    	$this->db->where('addr_code',$acode);
    	$query = $this->db->get('app_entity_address');
    	return $query->result();
    }

    public function get_dev_type(){
    	$query = $this->db->get('app_device_type_v');
		return $query->result();
    }

	public function get_dev_stats(){
		$query = $this->db->get('app_device_status_v');
		return $query->result();
	}
	
	public function get_item(){
		$query = $this->db->get('inv_items');
		return $query->result();
	}

    public function getAddr2($id){
    	$this->db->where('entity_id',$id);
    	$query = $this->db->get('app_entity_address');
    	return $query->result();
    }

    public function getEntity($id){
    	$this->db->where('entity_id',$id);
    	$query = $this->db->get('app_entity_v');
    	return $query->result();
    }

    public function get_e_type(){
    	$query = $this->db->get('entity_type_v');
    	return $query->result();    
    }

    public function get_acc_type(){
    	$query = $this->db->get('account_types_v');
    	return $query->result();  
    }

    public function get_acc_stats(){
    	$query = $this->db->get('account_status_v');
    	return $query->result();  
    }

    public function get_addr_pur(){
    	$query = $this->db->get('address_purpose_v');
    	return $query->result();  
    }

    public function getAllAddr($id,$acode){
    	$this->db->where('entity_id',$id);
    	$this->db->where('address_id',$acode);
    	$query = $this->db->get('app_entity_address');
    	return $query->result();
    }

    public function getAllAddr2($id){
    	$this->db->where('entity_id',$id);
    	$query = $this->db->get('app_entity_address');
    	return $query->result();
    }

    public function getacct2($id){
    	$this->db->where('entity_id',$id);
    	$query = $this->db->get('app_account_v2');
    	return $query->result();
    }

    public function getacct($id){
    	$this->db->where('entity_id',$id);
    	$query = $this->db->get('app_account_v2');
    	return $query->result();
    }

    public function getAllAccount($id,$acode,$addrcode){
    	$this->db->where('entity_id',$id);
    	$this->db->where('account_id',$acode);
    	$this->db->where('acct_addr',$addrcode);
    	$query = $this->db->get('app_account_v2');
    	return $query->result();
    }
    public function getEnaAddr($id){
    	$this->db->where('entity_id',$id);
    	$this->db->where('enable_flag','Y');
    	$query = $this->db->get('entity_address_v');
    	return $query->result();
    }
    public function getZones($id,$acode){
    	$this->db->where('entity_id',$id);
    	$this->db->where('addr_code',$acode);
    	$query = $this->db->get('entity_address_v');
    	return $query->result();
    }

    public function getSearch($etype,$ename){
    	  $sql;
	      if($etype != "" && $ename != ""){
	        $sql = "SELECT * FROM app_entity_v WHERE entity_type LIKE ".$this->db->escape($etype)." AND entity_name LIKE '".$ename."' ";
	      }else if($etype == "" && $ename != ""){
	        $sql = "SELECT * FROM app_entity_v WHERE entity_name LIKE ".$this->db->escape($ename)." ";
	      }else{
	        $sql = "SELECT * FROM app_entity_v WHERE entity_type LIKE ".$this->db->escape($etype)." ";
	      }
	      //$query = $this->db->get_where('app_modules', array('entity_type' => $etype, 'entity_name' => $ename), 1);
	      $query = $this->db->query($sql);
	      if($query->num_rows() != 0){
	        return $query->result();
	      }
	      else {
	        $result = "No results found.";
	        return $result;
	      }
    }
}